package com.axun.ckz.cricleviewdemo;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import java.text.DecimalFormat;



public class CircleProgressView extends View {

    private static final int DEFAULT_BGCIRCLE_COLOR=0xFFBBBBBB;
    private static final int DEFAULT_CIRCLE_SIZE = 10;
    private static final int DEFAULE_TEXT_SIZE = 14;
    private static final int DEFAULT_PROGRESS_COLOR = 0XFFFFFFFF;
    private static final int DEFAULT_TEXT_COLOR=0XFF333333;
    private static final float DEFAULT_START_ANGLE = 0;
    private static final float DEFAULT_DRAW_ANGLE = 360;
    private static final float DEFAULT_PROGRESS = 0;

    private Paint bgPaint;//底层圆弧画笔
    private Paint progressPaint;//进度圆弧画笔
    private Paint textPaint;//文字画笔

    private int circleSize = DEFAULT_CIRCLE_SIZE;//圆弧大小
    private int textSize = DEFAULE_TEXT_SIZE;//字体大小
    private int bgCircleColor = DEFAULT_BGCIRCLE_COLOR;//圆弧背景色
    private int progressColor = DEFAULT_PROGRESS_COLOR;//进度圆弧颜色
    private int textColor = DEFAULT_TEXT_COLOR;//字体颜色

    private boolean needPersent = false;//是否需要百分号
    private boolean needAni = false;//是否需要动画
    private boolean needProgress = false;//是否需要进度
    private float progress = DEFAULT_PROGRESS;//进度
    private float startAngle = DEFAULT_START_ANGLE;//开始角度
    private float drawAngle = DEFAULT_DRAW_ANGLE;//绘制角度


    private Context context;

    private int textWidth;
    private int textHeight;



    public CircleProgressView(Context context) {
        this(context,null);
    }

    public CircleProgressView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CircleProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.context = context;
        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.CircleProgressView);
        circleSize = array.getDimensionPixelSize(R.styleable.CircleProgressView_circleSize,DEFAULT_CIRCLE_SIZE);
        textSize = array.getDimensionPixelSize(R.styleable.CircleProgressView_textSize,DEFAULE_TEXT_SIZE);
        bgCircleColor = array.getColor(R.styleable.CircleProgressView_bgCircleColor,DEFAULT_BGCIRCLE_COLOR);
        progressColor = array.getColor(R.styleable.CircleProgressView_progressColor,DEFAULT_PROGRESS_COLOR);
        textColor = array.getColor(R.styleable.CircleProgressView_textColor,DEFAULT_TEXT_COLOR);
        needPersent = array.getBoolean(R.styleable.CircleProgressView_needPersent,false);
        needAni = array.getBoolean(R.styleable.CircleProgressView_needAni,false);
        needProgress = array.getBoolean(R.styleable.CircleProgressView_needProgress,false);
        progress = array.getFloat(R.styleable.CircleProgressView_progress,DEFAULT_PROGRESS);
        startAngle = array.getFloat(R.styleable.CircleProgressView_startAngle,DEFAULT_START_ANGLE);
        drawAngle = array.getFloat(R.styleable.CircleProgressView_drawAngle,DEFAULT_DRAW_ANGLE);
        array.recycle();



    }

    private void initPaint(){
        bgPaint = new Paint();
        bgPaint.setAntiAlias(true);
        bgPaint.setColor(bgCircleColor);
        bgPaint.setStyle(Paint.Style.STROKE);
        bgPaint.setStrokeWidth(circleSize);
        bgPaint.setStrokeCap(Paint.Cap.ROUND);

        progressPaint = new Paint();
        progressPaint.setAntiAlias(true);
        progressPaint.setColor(progressColor);
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setStrokeWidth(circleSize);
        progressPaint.setStrokeCap(Paint.Cap.ROUND);

        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setColor(textColor);
        textPaint.setTextSize(DisplayUtils.sp2px(context,textSize));

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        initPaint();

        float x = DisplayUtils.dp2px(context,10f);
        float y = DisplayUtils.dp2px(context,10f);
        RectF oval = new RectF( x, y,
                getWidth() - x, Math.min(getWidth(),getHeight())-y);
        //绘制底层圆环
        canvas.drawArc(oval, startAngle, drawAngle,false,bgPaint);
        //绘制进度圆环
        canvas.drawArc(oval, startAngle,(float)(progress*360/100),false,progressPaint);
        //绘制文字
        Rect rect = new Rect();
        String text;
        DecimalFormat df = new DecimalFormat("0.00");
        if (!needPersent){
            text = df.format(progress);
        }else {
            text = String.valueOf(df.format(progress))+"%";
        }
        textPaint.getTextBounds(text, 0, text.length(), rect);
        textWidth = rect.width();//文本的宽度
        textHeight = rect.height();//文本的高度
        if (needProgress){
            canvas.drawText(text,(getWidth()-textWidth)/2,(Math.min(getWidth(),getHeight())+textHeight)/2,textPaint);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureWidth(widthMeasureSpec));
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);

        Log.i("tag", "specMode:" + specMode + "  specSize:" + specSize + "  " + MeasureSpec.UNSPECIFIED + "  " + MeasureSpec.AT_MOST + "  " + MeasureSpec.EXACTLY);
    }

    private int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        DisplayMetrics dm2 = getResources().getDisplayMetrics();
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = dm2.widthPixels/2;
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }

        return result;
    }

    /**
     * 是否显示进度数字
     * @param needProgress
     */
    public void setNeedProgress(boolean needProgress) {
        this.needProgress = needProgress;
        invalidate();
    }

    /**
     * 是否需要百分号
     * @param needPersent
     */
    public void setNeedPersent(boolean needPersent) {
        this.needPersent = needPersent;
        invalidate();
    }

    /**
     * 是否允许动画
     * @param enableAni
     */
    public void setEnableAni(boolean enableAni){
        this.needAni = enableAni;
    }

    /**
     * 设置进度
     * @param progress
     */
    public void setProgress(float progress){
        if (!needAni){
            this.progress = progress;
            invalidate();
        }else {
            startAni(progress);
        }
    }

    /**
     * 设置圆弧宽度
     * @param circleWidth
     */
    public void setCircleWidth(int circleWidth){
        this.circleSize = DisplayUtils.dp2px(context,circleWidth);
        invalidate();
    }

    /**
     * 设置字体大小
     * @param textSize
     */
    public void setTextSize(int textSize){
        this.textSize = DisplayUtils.sp2px(context,textSize);
        invalidate();
    }

    /**
     * 设置进度条颜色
     * @param color
     */
    public void setProgressColor(int color){
        this.progressColor = color;
        invalidate();
    }

    /**
     * 设置进度条背景色
     * @param color
     */
    public void setProgressBgColor(int color){
        this.bgCircleColor = color;
        invalidate();
    }

    /**
     * 设置字体颜色
     * @param color
     */
    public void setTextColor(int color){
        this.textColor = color;
        invalidate();
    }



    private void startAni(float persent){
        ValueAnimator animator = ValueAnimator.ofFloat(0,persent);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                progress = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.setDuration(1000);
        animator.start();
    }

}